package controllers

import (
	"gini2p/logger"
	"gini2p/models"
	"gini2p/utility"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func Index(c *gin.Context) {
	page := c.DefaultQuery("page", "1")
	perpage := c.DefaultQuery("pageSize", "20")

	pagenum, _ := strconv.ParseInt(page, 10, 64)
	perpagenum, _ := strconv.ParseInt(perpage, 10, 64)
	where := utility.FormatSearchParams(c, "parkinfo")

	parklists, count, totalpage, err := models.GetParkList(pagenum, perpagenum, where)

	if err != nil {
		logger.Error("sql", "获取车场列表出错")
		c.JSON(http.StatusInternalServerError, gin.H{
			"status":  500,
			"message": "服务器出错,请稍后重试",
		})
		return
	}

	pageData := map[string]int64{
		"count":     count,
		"totalPage": totalpage,
		"page":      pagenum,
	}

	c.JSON(http.StatusOK, gin.H{
		"status":   200,
		"message":  "正常",
		"data":     parklists,
		"pageData": pageData,
	})
}
